package com.ylkj.user.controller;

import com.ylkj.common.util.bean.BeanMapUtil;
import com.ylkj.core.BaesController;
import com.ylkj.core.page.IGenericPage;
import com.ylkj.core.page.Page;
import com.ylkj.core.vo.JsonResult;
import com.ylkj.core.vo.QueryResult;
import com.ylkj.core.vo.TipMessage;
import com.ylkj.user.model.User;
import com.ylkj.user.services.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 */
@Controller
@RequestMapping(value = "/user/")
public class UserController extends BaesController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 地图数据
     *
     * @return
     */
    @RequestMapping(value = "mapdata.do")
    //@RequestMapping(value = "mapdata", method = RequestMethod.POST)
    @ResponseBody
    public Object mapdata(Map map, HttpServletRequest request) {
        logger.info("获取用户地图数据开始");
        List result = new ArrayList();
        try {
            //map = getQueryParams(request);
//            map = getQueryParams(request);
            User param = (User) BeanMapUtil.Map2Bean(User.class, map);
            result = userService.findListByMapData(param, null,null);
            return result;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return result;
        }finally {
            logger.info("获取用户地图数据结束");
        }
    }

    /**
     * 用户列表页(分页)
     *
     * @return
     */
    @RequestMapping(value = "page.do")
    //@RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public Object page(Map map, HttpServletRequest request) {
        logger.info("获取用户列表信息(分页):开始");
        try {
            map = getQueryParams(request);
            User param = (User) BeanMapUtil.Map2Bean(User.class, map);

            Page page = (Page) BeanMapUtil.Map2Bean(Page.class, map);
            IGenericPage<User> list = userService.findPageBy(param, page.getOffset(), page.getLimit(), "id", "asc");
            return this.buildQueryResult(true,list.getThisPageElements(),list.getTotalCount(),page.getPageSize(),page.getPage(), list.getTotalPage());
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.buildQueryResult(null,0);
        }finally {
            logger.info("获取用户列表信息(分页):结束");
        }
    }

    /**
     * 用户列表页
     *
     * @return
     */
    @RequestMapping(value = "list.do")
    //@RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(Map map, HttpServletRequest request) {
        logger.info("获取用户列表信息:开始");
        try {
            map = getQueryParams(request);
            User param = (User) BeanMapUtil.Map2Bean(User.class, map);
            List<User> list = userService.findListBy(param, "id", "asc");
            return list;
        } catch (Exception e) {
            // TODO: handle
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }finally {
            logger.info("获取用户列表信息:结束");
        }
    }

}
